/**
 * @author tsdy
 * @description
 * element中属性获取
 */
import { PropsResult } from '../typings/traverse';
/**
 *
 * @param ele
 * @param attrName
 * @param isSync 如果为true就只判断v-bind:attr.sync和:attr.sync这
 */
function getProps(ele: Element, attrName: string, isSync: boolean) {
  var goal: Partial<PropsResult> = {};
  if (isSync) {
    if (ele.hasAttribute(`:${attrName}.sync`)) {
      goal.path = ele.getAttribute(`:${attrName}.sync`);
      goal.isResponsive = true;
    } else if (ele.hasAttribute(`v-bind:${attrName}.sync`)) {
      goal.path = ele.getAttribute(`v-bind:${attrName}.sync`);
      goal.isResponsive = true;
    }
  } else {
    if (ele.hasAttribute(attrName)) {
      goal.path = ele.getAttribute(attrName);
      goal.isResponsive = false;
    } else if (ele.hasAttribute(`:${attrName}`)) {
      goal.path = ele.getAttribute(`:${attrName}`);
      goal.isResponsive = true;
    } else if (ele.hasAttribute(`v-bind:${attrName}`)) {
      goal.path = ele.getAttribute(`v-bind:${attrName}`);
      goal.isResponsive = true;
    }
  }
  return goal;
}

/**
 * 
 * @param name attrName
 * 从(@)v-on:attr中获取attr
 * 
 */
function getListenerName(name: string): string | undefined {
  let re = new RegExp(`(^v-on:)|(^@)`, 'g');
  if (name.search(re) == 0) {
    return name.replace(re, '');
  }
  return undefined;
}

export { getProps, getListenerName };
